package com.gw.quotes.net.service;

import org.ksoap2.serialization.SoapObject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.gw.quotes.entity.business.GetEstimateEntity;
import com.gw.quotes.net.NewException;
import com.gw.quotes.net.Request;
import com.gw.quotes.net.Response;
import com.yxw.util.StringUtils;

/**
 * 评价列表
 * 
 * @author yxw
 *
 */
public class GetEstimateServiceImpl extends WebBaseServiceImpl {

	@Override
	public Response setResponse() {
		return new GetEstimateEntity();
	}

	@Override
	public void checkParam(Request request) throws NewException {
		if (request == null) {
			throw new NewException(Response.RESULT_ERROR, "传入request不能为空");
		}
		if (StringUtils.isEmpty(request.getLoginname())) {
			throw new NewException(Response.RESULT_ERROR, "传入用户名不能为空");
		}
		if (StringUtils.isEmpty(request.getContractguid())) {
			throw new NewException(Response.RESULT_ERROR, "传入合同Contractguid不能为空");
		}
	}

	@Override
	public void setParamByRequest(Request request, SoapObject rpc)
			throws NewException {
		rpc.addProperty("arg0", request.getLoginname());
		rpc.addProperty("arg1", request.getTicket());
		rpc.addProperty("arg2", request.getContractguid());
	}

	@Override
	public String getMethodName() throws NewException {
		return "getEstimate";
	}

	@Override
	public void resolveResponse(Object object, Response response)
			throws NewException {
		try {
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
					.create();
			java.lang.reflect.Type type = new TypeToken<GetEstimateEntity>() {
			}.getType();
			GetEstimateEntity getEstimateEntity = gson.fromJson(
					object.toString(), type);
			if (getEstimateEntity != null) {
				setResponse(getEstimateEntity);
			} else {
				throw new Exception();
			}
		} catch (Exception e) {
			response.setMsg("获取失败");
			response.setResult(Response.RESULT_ERROR);
		}
	}

}
